//
//  DemoVarFuncArg.swift
//  swiftDemo
//
//  Created by JIENING ZHANG on 2022/12/1.
//  Copyright © 2022 lovivid. All rights reserved.
//

import UIKit

class DemoVarFuncArg: NSObject {
    
    // 不定 可变 函数参数 依次接收 年月日时分秒
    class func formatDate(_ yMdHms:Int...) -> Date? {
        
        var year:Int = 2000;
        var month:Int = 1;
        var day:Int = 1;
        var hour:Int = 0;
        var minute:Int = 0;
        var sec:Int = 0;
        
        print ("yMdHms=\(yMdHms)");
        
        var argIndex = 0;
        for num in yMdHms {
            switch (argIndex) {
            case 0: year = num;
            case 1: month = num;
            case 2: day = num;
            case 3: hour = num;
            case 4: minute = num;
            case 5: sec = num;
            default:
                1==1;
            }
            argIndex += 1;
        }
        
        let dc = DateComponents(year:year, month: month, day: day, hour: hour, minute: minute, second: sec);
        //创建一个转换桥梁
        let cal = Calendar.current;
        //进行转换
        return cal.date(from: dc);
    }
    
    class func demoLogic() {
        let df = DateFormatter()
        df.dateFormat = "yyyy-MM-dd HH:mm:ss z";
        
        print(df.string(from: (formatDate() ?? Date())));
        print(df.string(from: (formatDate(2022) ?? Date())));
        print(df.string(from: (formatDate(2022, 12) ?? Date())));
        print(df.string(from: (formatDate(2022, 12, 26) ?? Date())));
        print(df.string(from: (formatDate(2022, 12, 26, 13) ?? Date())));
        print(df.string(from: (formatDate(2022, 12, 26, 13, 54) ?? Date())));
        print(df.string(from: (formatDate(2022, 12, 26, 13, 54, 33) ?? Date())));
    }
}
